import 'dart:io';
import 'package:baselib/utils/file_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/**
 * 图片加载管理类
 */
class ImageUtil{
  //判断是否是图片
  static isImg(String url,{List<String>? addSuffixs}) {
    String fileNameSuffix = FileUtil.getFileNameSuffix(fileName: FileUtil.getFileName(filePath: url));
    return [
      'jpg', 'jpeg','png',
      'JPG','JPEG','PNG',
      ...(addSuffixs??[])
    ].contains(fileNameSuffix);
  }
  static bool isURL(String str) {
    if((str??'').isNotEmpty&&(str.startsWith('http'))){
      return true;
    }
    return false;
  }
  //统一加载图片
  static Widget loadImage(String imgPath,
      {Key? key,BorderRadius? radius,String? placeholder,String? errorholder,
        double? width, double? height, BoxFit? fit,String? package}){

    if((imgPath??'').startsWith('http')){
     return _loadNetworkImage(imgPath,key:key,width: width,height: height,
         radius: radius??BorderRadius.zero,fit: fit??BoxFit.cover,
         placeholder: placeholder,errorholder: errorholder
     );
    }else{
      return _loadAssetImage(imgPath,key: key,width: width,height: height,
          radius: radius??BorderRadius.zero,fit: fit??BoxFit.cover,
          errorholder: errorholder,
          package: package);
    }
  }
  /// 加载本地资源图片
  static Widget _loadAssetImage(String imgPath, {Key? key,double? width, double? height,
      BoxFit? fit,BorderRadius? radius,Color? color,String? errorholder,String? package}){

    Image image = Image.asset(
      imgPath??'', height: height, width: width, fit: fit,color: color,package: package,
      errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace,){
        return ((errorholder=='none')||(errorholder??'').isEmpty)?
        Container(child: Icon(
          Icons.image,
          color: Color(0xffeeeeee),
          size: width,
        ),):
        _loadAssetImage(errorholder!, height: height, width: width, fit: fit??BoxFit.cover,package: package);
      },
    );
    final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
    stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
    },onError: (dynamic exception, StackTrace? stackTrace) {
    } ));

    return ClipRRect(
      key: key,
      borderRadius: radius??BorderRadius.zero,
      child: image,
    );
  }
  //加载图片文件
  static Widget loadImageFile(File file,
      {double? width, double? height, BoxFit? fit,double? roundRadius,String? errorholder,String? package}){
    Image image = Image.file(
      file, height: height, width: width, fit: fit??BoxFit.cover,
      errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace,){
        return ((errorholder=='none')||(errorholder??'').isEmpty)?
        Container(child: Icon(
          Icons.image,
          color: Color(0xffeeeeee),
          size: width,
        ),):
        _loadAssetImage(errorholder!, height: height, width: width, fit: fit??BoxFit.cover,package: package);
      },
    );
    final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
    stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
    },onError: (dynamic exception, StackTrace? stackTrace) {
    } ));

    return ClipRRect(
      borderRadius: BorderRadius.circular(roundRadius??0),
      child: image,
    );
  }

  /// 加载网络图片
  static Widget _loadNetworkImage(String imgPath,
      {Key? key,BorderRadius? radius,
    String? placeholder,String? errorholder, double? width, double? height, BoxFit? fit,String? package}){
    return Container(
      width: width,
      height: height,
      child: ClipRRect(
        borderRadius: radius??BorderRadius.zero,
        child: CachedNetworkImage(
          key: key,
          imageUrl: imgPath??'',
          placeholder: (context, url) => ((placeholder=='none')||(placeholder??'').isEmpty)?CupertinoActivityIndicator()
              :_loadAssetImage(placeholder!, height: height, width: width, fit: fit??BoxFit.cover,package: package),

          errorWidget: (context, url, error) => ((errorholder=='none')||(errorholder??'').isEmpty)?
          Container(child: Icon(
              Icons.image,
            color: Color(0xffeeeeee),
            size: width,
          ),):
          _loadAssetImage(errorholder!, height: height, width: width, fit: fit??BoxFit.cover,package: package),
          width: width,
          height: height,
          fit: fit??BoxFit.cover,
        )
      ),
    );
  }
  /// 加载网络图片
  static Widget FadeInImageAssetNetwork(String imgPath,
      {String? placeholder, double? width, double? height, BoxFit? fit}){
    print(imgPath);
    return FadeInImage.assetNetwork(
      image: imgPath == null ? "" : imgPath,
      placeholder:placeholder??"none",
      width: width,
      height: height,
      fit: fit??BoxFit.cover,
    );
  }

}
